package io.renren.com.qywt.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import io.renren.com.qywt.dao.EjectFeeDao;
import io.renren.com.qywt.dto.AftersaleFinanceCollectDTO;
import io.renren.com.qywt.dto.ContractDTO;
import io.renren.com.qywt.dto.MaxContractDTO;
import io.renren.com.qywt.dto.ShipmentInfoDTO;
import io.renren.com.qywt.entity.AftersaleEjectFeeEntity;
import io.renren.commons.dynamic.datasource.annotation.DataSource;
import io.renren.commons.mybatis.service.impl.BaseServiceImpl;
import io.renren.commons.security.user.UserDetail;
import io.renren.commons.tools.enums.DeleteEnum;
import io.renren.commons.tools.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.util.*;

@Service
public class TaskEjectFeeService extends BaseServiceImpl<EjectFeeDao, ShipmentInfoDTO> {
    @Autowired
    private AftersaleEjectFeeService ejectFeeService;
    @Autowired
    private AftersaleFinanceCollectService aftersaleFinanceCollectService;
    @Autowired
    private TaskContractService contractService;

    @DataSource("slave1")
    @Transactional
    public List<AftersaleEjectFeeEntity> getShipmentInfoList(UserDetail userDetail,AftersaleFinanceCollectDTO dto){
        Map<String,Object> params=new HashMap<>();
        Date collectDate=new Date();
        if(ObjectUtil.isNull(dto)){
            collectDate=DateUtils.addDateDays(new Date(),-1);
        }else{
            collectDate=dto.getCollectDate();
        }
        String collectDateStr=DateUtils.format(collectDate,DateUtils.DATE_PATTERN);
        params.put("collectDate",collectDateStr);
        List<ShipmentInfoDTO> shipmentInfoList=baseDao.getShipmentInfoList(params);
        List<AftersaleEjectFeeEntity> aftersaleEjectFeeEntityList=new ArrayList<>();

        /**
         * 临时变量
         */
        double weightFee=0.0;
        double numFee=0.0;
        double cubeFee=0.0;

        for(ShipmentInfoDTO shipmentInfoDTO:shipmentInfoList){
            ContractDTO contractDTO = contractService.getContractBySkuId(shipmentInfoDTO.getSkuId());
            if(ObjectUtil.isNotNull(contractDTO)) {
                if (contractDTO.getNeedMax() == 1) {//需要修正值
                    MaxContractDTO maxContractDTO = contractService.getMaxContractBySkuId(shipmentInfoDTO.getSkuId());
                    if(ObjectUtil.isNotNull(maxContractDTO)){
                        weightFee=shipmentInfoDTO.getShippedQty()*shipmentInfoDTO.getSkuWeight()*maxContractDTO.getPickWeight();
                        numFee=shipmentInfoDTO.getShippedQty()*maxContractDTO.getPickNum();
                        cubeFee=shipmentInfoDTO.getShippedQty()*(shipmentInfoDTO.getSkuCube()/1000000)*maxContractDTO.getPickCube();

                        double max=Collections.max(Arrays.asList(weightFee,numFee,cubeFee));
                        if(max==weightFee){//按照重量计算
                            shipmentInfoDTO.setRecordType(2);
                        }else if(max==numFee){//按照数量收费
                            shipmentInfoDTO.setRecordType(1);
                        }else if(max==cubeFee){
                            shipmentInfoDTO.setRecordType(3);
                        }
                        shipmentInfoDTO.setMaxCost(max);
                    }
                }
                // 按件计费，包括首件含以下，最低计费；首件以上每件按续件费用计费
                Integer pickNum = contractDTO.getPickNum();//拣货首件数
                double pickFee = contractDTO.getPickFee();//拣货首件费
                double pickAdd = contractDTO.getPickAdd();//拣货续件费
                if (shipmentInfoDTO.getShippedQty() <= pickNum) {
                    shipmentInfoDTO.setTotalCost(shipmentInfoDTO.getShippedQty()*pickFee);
                } else {
                    shipmentInfoDTO.setTotalCost(shipmentInfoDTO.getShippedQty()*pickFee+(shipmentInfoDTO.getShippedQty()-pickNum)*pickAdd);
                }
            }
            //入库费明细
            AftersaleEjectFeeEntity entity= new AftersaleEjectFeeEntity();
            entity.setBatchNo(collectDateStr.replaceAll("-",""));
            entity.setEjectNo(shipmentInfoDTO.getShipmentKey());
            entity.setReferenceNo(shipmentInfoDTO.getReferNo());
            entity.setOrderNum(shipmentInfoDTO.getShippedQty());
            entity.setEjectFee(BigDecimal.valueOf(shipmentInfoDTO.getTotalCost()).setScale(2,BigDecimal.ROUND_HALF_DOWN));
            entity.setStatus(1);
            entity.setDelFlag(DeleteEnum.NO.value());
            entity.setTenantCode(userDetail.getTenantCode());
            entity.setCreator(userDetail.getId());
            aftersaleEjectFeeEntityList.add(entity);
        }
        return aftersaleEjectFeeEntityList;
    }
}
